<?php
// +----------------------------------------------------------------------
// | Author: 黄柯柯 <keke.huang@ostengar.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use cmf\controller\UserBaseController;

class ShareCodeController extends UserBaseController
{
    /**
     * 分享网站 生成二维码
     * @param array $params
     * @param string $params['url'] 当前页面连接
     * @return $codeUrl 生成图片的路径
     */
    public function share()
    {

        $params = $this->request->param();
        $url    = $params['url']; //将url地址写好
        if (empty($url)) {
            $this->error('操作失败，请稍后再试！');
        }
        Vendor('phpqrcode.phpqrcode'); //加载类库

        $errorCorrectionLevel = 'L'; //容错级别
        $i                    = 320;
        $j                    = floor($i / 37 * 100) / 100 + 0.01;
        $matrixPointSize      = $j; //生成图片大小

        //生成二维码图片
        $savePath = "upload" . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
        $name     = time() . rand(100, 999) . '.png';
        $filename = $savePath . $name;
        $QRcode   = new \QRcode(); //实例化对象

        $QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);

        $QR = $filename; //已经生成的原始二维码图片文件

        $QR      = imagecreatefromstring(file_get_contents($QR));
        $codeUrl = 'code/' . $name;
        $this->success('二维码生成成功！', '', cmf_get_image_url($codeUrl));
    }

}
